package com.jrx.anytxn.reporting.job.lsFile.config;

import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.lsFile.step.LSCustomerFileListener;
import com.jrx.anytxn.reporting.job.lsFile.step.LSCustomerFileProcessor;
import com.jrx.anytxn.reporting.job.lsFile.step.LSCustomerFileWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成个人信息文件(按照更新时间 增量抽取)
 *
 * @author liuyang
 * @date 2021-03-13
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateLSCustomerFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateLSCustomerFileConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private LSCustomerFileListener customerFileListener;
    @Autowired
    private LSCustomerFileProcessor customerFileProcessor;
    @Autowired
    private LSCustomerFileWriter customerFileWriter;

    @Bean
    public Job generateLSCustomerFileJob() {
        return jobBuilderFactory.get("generateLSCustomerFileJob").incrementer(new RunIdIncrementer()).listener(customerFileListener)
                .start(generateLSCustomerFileStep())
                .build();
    }

    @Bean
    public Step generateLSCustomerFileStep() {
        return stepBuilderFactory.get("generateLSCustomerFileStep")
                .<CmCustomerFirstLevel, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(customerLSFileReader())
                .processor(customerFileProcessor)
                .writer(customerFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> customerLSFileReader() {
        logger.info("生成临商个人信息文件批量启动,读取个人信息，机构：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }
}
